package com.zhongfl.crm.lixiao.bean.resolve;

import com.heptagram.lixiao.bean.LiXiaoConsts;
import com.heptagram.lixiao.bean.response.LXUserResponse;
import com.heptagram.lixiao.bean.response.revisitlog.LXRevisitLogResponse;
import com.zhongfl.crm.enums.boss.OrderMemorandumAtNoticeSendStatusEnum;
import com.zhongfl.crm.enums.boss.OrderMemorandumStageEnum;
import com.zhongfl.crm.enums.boss.OrderMemorandumSyncStatusEnum;
import com.zhongfl.crm.enums.boss.OrderMemorandumTypeEnum;
import com.zhongfl.crm.lixiao.mapper.LXRevisitLogMapper;
import com.zhongfl.crm.model.LxUser;
import com.zhongfl.crm.model.boss.LxOrder;
import com.zhongfl.crm.model.boss.Order;
import com.zhongfl.crm.model.boss.OrderMemorandum;
import com.zhongfl.crm.service.LxUserService;
import com.zhongfl.crm.service.boss.LXOrderService;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

/**
 * @author wang.yuchuan
 * @date 2018/7/1
 */
public class OrderMemorandumModelResolver implements ModelResolver<OrderMemorandum, LXRevisitLogResponse.RevisitLog> {

    private LxOrder lxOrder;

    private LxUserService lxUserService;

    private LXRevisitLogMapper lxRevisitLogMapper;

    private LXOrderService lxOrderService;

    private static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");


    public OrderMemorandumModelResolver(LxOrder lxOrder,
                                        LxUserService lxUserService,
                                        LXRevisitLogMapper lxRevisitLogMapper,
                                        LXOrderService lxOrderService) {
        this.lxOrder = lxOrder;
        this.lxUserService = lxUserService;
        this.lxRevisitLogMapper = lxRevisitLogMapper;
        this.lxOrderService = lxOrderService;
    }

    @Override
    public OrderMemorandum resolve(LXRevisitLogResponse.RevisitLog obj) throws ModelResolverException {
        if (Objects.isNull(lxOrder)) {
            throw new ModelResolverException("lxOrder为Null");
        }
        if (StringUtils.isEmpty(lxOrder.getOrderNo())) {
            throw new ModelResolverException("lxOrderId: " + lxOrder.getLxId() + " 本地客户订单号为空");
        }

        OrderMemorandum orderMemorandum = new OrderMemorandum();

        // 修改一：保存客户订单号
        if(lxOrder.getOrderType().equals(LiXiaoConsts.QueryEntity.opportunity)){
            orderMemorandum.setOrderNo(lxOrder.getOrderNo());
            orderMemorandum.setCustomerOrderNo(lxOrder.getOrderNo());
        }
        if("contract".equals(lxOrder.getOrderType())){
            orderMemorandum.setOrderNo(lxOrder.getOrderNo());

            Order order = lxOrderService.findByOrderNo(lxOrder.getOrderNo());
            orderMemorandum.setCustomerOrderNo(order.getCustomerOrderNo());
        }
        //orderMemorandum.setOrderNo(lxOrder.getOrderNo());

        LXUserResponse.User createUser = obj.getUser();

        LxUser lxUser = lxUserService.findByLxId(createUser.getId());
        if (Objects.isNull(lxUser)) {
            throw new ModelResolverException("lx用户id: " + createUser.getId() + " 在用户表找不到");
        }
        if (StringUtils.isEmpty(lxUser.getStaffName())) {
            orderMemorandum.setStaffName(lxUser.getName());
        } else {
            orderMemorandum.setStaffName(lxUser.getStaffName());
        }

        orderMemorandum.setLxRevisitLogId(obj.getId());
        orderMemorandum.setLxLoggableId(obj.getLoggableId());
        orderMemorandum.setLxLoggableType(obj.getLoggableType());

        orderMemorandum.setAvatarUrl(obj.getUser().getAvatarUrl());
        if(StringUtils.isNotEmpty(obj.getCategory())){
            orderMemorandum.setStage(OrderMemorandumStageEnum.getEnum(obj.getCategory()));
        }

        //String content = StringUtils.replaceAll(obj.getContent(), "\r\n", "\\\\r\\\\n");
        orderMemorandum.setContent(obj.getContent());
        try {
            orderMemorandum.setOperateTime(DATE_TIME_FORMAT.parse(obj.getCreateAt()));
        } catch (ParseException e) { }
        orderMemorandum.setType(OrderMemorandumTypeEnum.REMARK);
        orderMemorandum.setModifiedBy(lxUser.getStaffName());
        orderMemorandum.setSyncStatus(OrderMemorandumSyncStatusEnum.SUCCESS_SYNC);
        orderMemorandum.setAtNoticeSendStatus(OrderMemorandumAtNoticeSendStatusEnum.SUCCESS_SEND);

//        List<LXRevisitLogResponse.AtUser> atUsers = obj.getAtUsers();
//        List<String> list = new ArrayList<>(10);
//        for (LXRevisitLogResponse.AtUser atUser : atUsers) {
//            list.add(lxUserMapper.selectByLxId(atUser.getUserId()).getStaffCode());
//        }
//        orderMemorandum.setAtStaffs(StringUtils.join(list, ","));
//        orderMemorandum.setSendMessage(false);

        return orderMemorandum;
    }

    public OrderMemorandum resolveComment(LXRevisitLogResponse.RevisitLog revisitLog, LXRevisitLogResponse.Comment comment) {
        OrderMemorandum orderMemorandum = new OrderMemorandum();

        // 修改一：保存客户订单号
        //orderMemorandum.setOrderNo(this.lxOrder.getOrderNo());
        if(lxOrder.getOrderType().equals(LiXiaoConsts.QueryEntity.opportunity)){
            orderMemorandum.setOrderNo(lxOrder.getOrderNo());
            orderMemorandum.setCustomerOrderNo(lxOrder.getOrderNo());
        }
        if("contract".equals(lxOrder.getOrderType())){
            orderMemorandum.setOrderNo(lxOrder.getOrderNo());

            Order order = lxOrderService.findByOrderNo(lxOrder.getOrderNo());
            orderMemorandum.setCustomerOrderNo(order.getCustomerOrderNo());
        }

        LxUser lxUser = lxUserService.findByLxId(comment.getUser().getId());
        if (Objects.isNull(lxUser)) {
            throw new ModelResolverException("lx用户id: " + comment.getUser().getId() + " 在销售表找不到");
        }
        if (StringUtils.isEmpty(lxUser.getStaffName())) {
            orderMemorandum.setStaffName(lxUser.getName());
        } else {
            orderMemorandum.setStaffName(lxUser.getStaffName());
        }

        orderMemorandum.setAvatarUrl(comment.getUser().getAvatarUrl());
        orderMemorandum.setStage(OrderMemorandumStageEnum.getEnum(revisitLog.getCategory()));
        //orderMemorandum.setOrderStatus();
        orderMemorandum.setContent(comment.getContent());
        try {
            orderMemorandum.setOperateTime(DATE_TIME_FORMAT.parse(comment.getCreatedAt()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        orderMemorandum.setModifiedBy(lxUser.getStaffName());

        orderMemorandum.setLxRevisitLogId(comment.getId());
        orderMemorandum.setLxLoggableId(revisitLog.getId());
        orderMemorandum.setLxLoggableType(revisitLog.getLoggableType());
        orderMemorandum.setLxRevisitLogParentId(revisitLog.getId());
        orderMemorandum.setParentId(lxRevisitLogMapper.selectByLXRevisitLogId(revisitLog.getId()).getId());
        orderMemorandum.setType(OrderMemorandumTypeEnum.COMMENT);
        orderMemorandum.setSyncStatus(OrderMemorandumSyncStatusEnum.SUCCESS_SYNC);
        orderMemorandum.setAtNoticeSendStatus(OrderMemorandumAtNoticeSendStatusEnum.SUCCESS_SEND);

        return orderMemorandum;
    }
}
